<?php
    include "../classes/logincheck.php";
    include('../classes/conn.php');
    $class_num=$_POST['class_num'];
    $class_name=$_POST['class_name'];
    if(islogin()=="teacher_login"){
        $stu_repeat=0;

//        若不上传学生名单，则直接创建班级
        if(empty($_FILES['name_list']['tmp_name']))
        {
            //            判断班级是否重复
            $query_class = "select num from class where num='{$class_num}'";
            $res = mysql_query($query_class);
            if (mysql_num_rows($res)) {
                die("class_exist");
            } else {

//            插入班级
                $query = "insert into class (num,name,sum,teacher) values ('{$class_num}','{$class_name}','0','{$_SESSION['t_num']}')";
                mysql_query($query);
            }
                die('empty_file');
        }

//        判断上传文件的格式和大小
        else if(($_FILES['name_list']['type']!="application/vnd.ms-excel")
            &&($_FILES['name_list']['type']!="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")){
            echo "type_limit";
            exit();
        }
        else if($_FILES['name_list']['size']>2*1024*0124){
            echo "size_limit";
            exit();
        }

//         已经上传文件且格式和大小正确
        else {
            move_uploaded_file($_FILES['name_list']['tmp_name'],
                "../Files/" . $_SESSION['t_num'] . "/namelist/" . $_FILES['name_list']['name']);
            $dir="requests/Files/". $_SESSION['t_num'] . "/namelist/" . $_FILES['name_list']['name'];
            include "../classes/handle_excel.php";

//            判断班级是否重复
            $query_class = "select num from class where num='{$class_num}'";
            $res = mysql_query($query_class);
            if (mysql_num_rows($res)) {
                die("class_exist");
            } else {

//            插入班级
                $sum=count($class_num_array);
                $query = "insert into class (num,name,teacher,namelist) values ('{$class_num}','{$class_name}','{$_SESSION['t_num']}','{$dir}')";
                mysql_query($query);

//            插入学生
                for ($i = 0; $i < count($class_num_array); $i++) {
                    $psw = md5(substr($class_num_array[$i], -6));
                    $query2 = "insert into student (num,psw,name,class,teacher) values
('{$class_num_array[$i]}','{$psw}','{$class_name_array[$i]}','{$class_num}','{$_SESSION['t_num']}')";

                    //            判断学生是否存在
                    $query_exist = "select num from student where num='{$class_num_array[$i]}' and class='{$class_num}' and teacher='{$_SESSION['t_num']}'";
                    $res=mysql_query($query_exist);
                    if($class_num_array[$i]!=null) {
                        if (mysql_num_rows($res)>=1) {
                            $stu_repeat++;
                        } else {
                            mysql_query($query2);
                        }
                    }
                }
                if($stu_repeat>0){
                    echo "stu_repeat";
                }
                else{
                    echo "1";
                }
                mysql_close($conn);
            }
        }
    }
    else{
        echo "no_login";
    }

